﻿using AspectSharp;

namespace Mbs.AOP
{
    public class ProxyFactory:Mbs.ComponentModel.Container.Proxy.IProxyFactory
    {
        private readonly IAspectEngine AspectEngine;

        public ProxyFactory(IAspectEngine aspectEngine)
        {
            AspectEngine = aspectEngine;
        }

        public object Create(Mbs.ComponentModel.Container.Context.ICreateContext ctx, object instance)
        {
            instance = AspectEngine.WrapClass(ctx.Component.Implementation);
            return instance;
        }

        public bool RequiresTargetInstance(Mbs.ComponentModel.Container.IKernel kernel, Mbs.ComponentModel.Container.Metadata.IComponentInfo model)
        {
            return false;
        }
        
        public bool ShouldCreateProxy(Mbs.ComponentModel.Container.Metadata.IComponentInfo model)
        {
            return model.ExtendedProperties.Contains("proxy");
        }
    }
}
